package com.sakura.mall.coupon.couponSpuRelation.controller;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.common.response.R;
import com.sakura.mall.coupon.couponSpuRelation.domain.request.AddCouponSpuRelationRequest;
import com.sakura.mall.coupon.couponSpuRelation.domain.request.UpdateCouponSpuRelationRequest;
import com.sakura.mall.coupon.couponSpuRelation.domain.request.CouponSpuRelationPageRequest;
import com.sakura.mall.coupon.couponSpuRelation.service.CouponSpuRelationService;
import com.sakura.mall.coupon.couponSpuRelation.domain.vo.CouponSpuRelationVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券与产品关联
 *
 * @author sakura
 */
@RestController
@RequestMapping("/couponSpuRelation")
@RequiredArgsConstructor
public class CouponSpuRelationController {

    private final CouponSpuRelationService couponSpuRelationService;


    /**
     * 新建
     *
     * @return R {@link Void}
     */
    @PostMapping
    public R<Void> create(@RequestBody @Validated AddCouponSpuRelationRequest request) {
        couponSpuRelationService.add(request);
        return R.ok();
    }


    /**
     * 分页获取
     *
     * @return R {@link Page<CouponSpuRelationVo>}
     */
    @GetMapping("list")
    public R<Page<CouponSpuRelationVo>> page(CouponSpuRelationPageRequest request) {
        Page<CouponSpuRelationVo> ret = couponSpuRelationService.queryPage(request);
        return R.ok(ret);
    }


    /**
     * 根据id删除
     *
     * @return R {@link Void}
     */
    @DeleteMapping("{id}")
    public R<Void> remove(@PathVariable Long id) {
        couponSpuRelationService.removeById(id);
        return R.ok();
    }


    /**
     * 根据id更新
     *
     * @return R {@link Void}
     */
    @PostMapping("{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody UpdateCouponSpuRelationRequest request) {
        couponSpuRelationService.updateById(id, request);
        return R.ok();
    }

}
